package com.quantanetwork.esm.port;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
* Company: QuantaNetwork BeiJing Ltd
* Author : 张彦龙
* DateTime: Nov 26, 2008  1:38:06 PM
*/
public class QuantaThreadPool extends ThreadPoolExecutor
{
	public static Logger logger = Logger.getLogger(QuantaThreadPool.class.getName());

	@Override
	protected void beforeExecute(Thread t, Runnable r)
	{
		logger.info("Current Thread:" + t.getName());
		super.beforeExecute(t, r);
	}

	public QuantaThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
	{
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
	}

	public static ExecutorService newFixedThreadPool(int nThreads)
	{
		return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
	}

	public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
	{
		return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
	}

	public static ExecutorService newCachedThreadPool()
	{
		return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t)
	{
		logger.info("Current Thread:" + r.getClass().getName());
		super.afterExecute(r, t);
	}
}
